package com.leyou.item.service.impl;

import com.leyou.item.interfaces.pojo.Goods;
import com.leyou.item.interfaces.pojo.GoodsDetail;
import com.leyou.item.interfaces.pojo.Sku;
import com.leyou.item.interfaces.pojo.Stock;
import com.leyou.item.interfaces.pojo.vo.GoodsPage;
import com.leyou.item.mapper.GoodsDetailMapper;
import com.leyou.item.mapper.GoodsMapper;
import com.leyou.item.mapper.SkuMapper;
import com.leyou.item.mapper.StockMapper;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 新增商品的业务逻辑----->涉及多张表的新增
 * @version 1.0.0
 * @Author yong.Mr
 * @data 2019-8-9
 */
@Service
@Transactional
public class SpuServiceImpl implements SpuService {

    /**
     * 注入GoodsMapper
     */
    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * 注入SkuMapper
     */
    @Autowired
    private SkuMapper skuMapper;

    /**
     * 注入GoodsDetail
     */
    @Autowired
    private GoodsDetailMapper  goodsDetailMapper;

    /**
     * 注入StockMapper
     */
    @Autowired
    private StockMapper stockMapper;

    /**
     * 新增tb_spu和tb_stock表的数据
     * @param goodsPage 所需要的数据
     */
    @Override
    public void saveGoods(GoodsPage goodsPage) {
        System.out.println("测试数据----->:" + goodsPage.toString());
        // 新增spu
        // 设置默认字段
        Goods goods = new Goods();
        goods.setId(null);
        goods.setTitle(goodsPage.getTitle());
        goods.setSubTitle(goodsPage.getSubTitle());
        goods.setCid1(goodsPage.getCid1());
        goods.setCid2(goodsPage.getCid2());
        goods.setCid3(goodsPage.getCid3());
        goods.setBrandId(goodsPage.getBrandId());
        goods.setSaleable(true);
        goods.setValid(true);
        goods.setCreateTime(new Date());
        goods.setLastUpdateTime(goods.getCreateTime());
        goodsMapper.insertSelective(goods);
        goodsPage.setId(goods.getId());

        // 新增spuDetail
        GoodsDetail goodsDetail = goodsPage.getSpuDetail();
        goodsDetail.setSpuId(goods.getId());
        this.goodsDetailMapper.insertSelective(goodsDetail);

    }

    /**
     * 新增tb_sku和tb_stock表的数据
     * @param goodsPage 所需要的数据
     */
    @Override
    public void saveSkuAndStock(GoodsPage goodsPage) {
        goodsPage.getSkus().forEach(sku -> {
            // 新增sku
            sku.setSpuId(goodsPage.getId().longValue());
            sku.setCreateTime(new Date());
            sku.setLastUpdateTime(sku.getCreateTime());
            this.skuMapper.insertSelective(sku);

            // 新增库存
            Stock stock = new Stock();
            stock.setSkuId(sku.getId());
            stock.setStock(sku.getStock());
            this.stockMapper.insertSelective(stock);
        });

    }

    /**
     * 根据spuId查询spuDetail
     * @param spuId spuId 接收前台传入的参数列表
     * @return ResponseEntity<GoodsDetail>
     */
    @Override
    public GoodsDetail querySpuDetailBySpuId(Long spuId) {
        GoodsDetail goodsDetail = goodsDetailMapper.selectByPrimaryKey(spuId);
        return goodsDetail;
    }

    /**
     * 根据spuId查询sku的集合
     * @param spuId
     * @return
     */
    @Override
    public List<Sku> querySkusBySpuId(Long spuId) {
        Sku sku = new Sku();
        sku.setSpuId(spuId);
        List<Sku> skus = this.skuMapper.select(sku);
        skus.forEach(s -> {
            Stock stock = this.stockMapper.selectByPrimaryKey(s.getId());
            s.setStock(stock.getStock());
        });
        return skus;
    }

    /**
     * 编辑商品
     * @param goodsPage
     * @return
     */
    @Override
    public void updateGoods(GoodsPage goodsPage) {
        // 查询以前sku
        Sku record = new Sku();
        record.setSpuId(goodsPage.getId());
        List<Sku> skus = this.skuMapper.select(record);
        // List<Sku> skus = this.querySkusBySpuId(goodsPage.getId());
        // 如果以前存在，则删除
        if(!CollectionUtils.isEmpty(skus)) {
            List<Long> ids = skus.stream().map(s -> s.getId()).collect(Collectors.toList());
            // 删除以前库存
            Example example = new Example(Stock.class);
            example.createCriteria().andIn("skuId", ids);
            this.stockMapper.deleteByExample(example);

            // 删除以前的sku
            Sku records = new Sku();
            record.setSpuId(goodsPage.getId());
            this.skuMapper.delete(records);

        }
        // 新增sku和库存
        this.saveSkuAndStock(goodsPage);

        // 更新spu
        Goods goods = new Goods();
        goods.setId(goodsPage.getId());
        goods.setTitle(goodsPage.getTitle());
        goods.setSubTitle(goodsPage.getSubTitle());
        goods.setCid1(goodsPage.getCid1());
        goods.setCid2(goodsPage.getCid2());
        goods.setCid3(goodsPage.getCid3());
        goods.setBrandId(goodsPage.getBrandId());
        goods.setSaleable(null);
        goods.setValid(null);
        goods.setCreateTime(null);
        goods.setLastUpdateTime(new Date());
        this.goodsMapper.updateByPrimaryKeySelective(goods);

        // 更新spu详情
        this.goodsDetailMapper.updateByPrimaryKeySelective(goodsPage.getSpuDetail());
    }

    @Override
    public Goods querySpuById(Long id) {
        return this.goodsMapper.selectByPrimaryKey(id);
    }
}
